package com.reach.redis.interceptor;

import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.reach.redis.annotation.CacheEvict;
import com.reach.redis.aspect.CacheAspect;
import com.reach.redis.bo.CacheConfig;
import com.reach.redis.manager.SpringContextUtil;

 
/**
 * 缓存拦截
 */

public class ModificationCacheInterceptor extends AbstractCacheInterceptor {
	
	private static final Logger logger = LoggerFactory.getLogger(ModificationCacheInterceptor.class);
	
	public Object intercept(MethodInvocation invo,CacheConfig cacheConfig) throws Throwable {
			
		CacheAspect cacheAspect = (CacheAspect) SpringContextUtil.getBean("cacheAspect");
		final String cacheKey = getKey(invo.getArguments(),cacheConfig);
		cacheAspect.delete(getCache(cacheConfig).module(),true,cacheKey);
		Object result = invo.proceed();
		return result;
	}

	@Override
	public String[] getAnnotation(CacheConfig cacheConfig) {
	
		return getTrim(getCache(cacheConfig).key().split(","));
	}
	
	public CacheEvict getCache(CacheConfig cacheConfig) {
		CacheEvict cacheable = (CacheEvict) cacheConfig.getCache();
		return cacheable;
	}

	@Override
	public String getModule(CacheConfig cacheConfig) {
		return getCache(cacheConfig).module();
	}

	@Override
	public String getEvent(CacheConfig cacheConfig) {
		return getCache(cacheConfig).event();
	}


}